import { ApiResponse, Article } from '@/types/data'
import { ChannelAction, RootThunkAction } from '@/types/store'
import request from '@/utils/request'

/**
 * channel_id 是 频道ID
   timestamp  是 时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳
 * @returns
 */
export const getArticleList = (channelId:number, timestamp: number): RootThunkAction => {
  return async (dispatch) => {
    // eslint-disable-next-line camelcase
    const res = await request.get<ApiResponse<{pre_timestamp:number, results: Article[]}>>('articles', { params: { channel_id: channelId, timestamp } })
    console.log(res)
    // res.data.data.results
    dispatch({
      type: 'article/setArticle',
      payload: {
        channelId: channelId,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}

export const getNewArticleList = (channelId:number): RootThunkAction => {
  return async (dispatch) => {
    // eslint-disable-next-line camelcase
    const res = await request.get<ApiResponse<{pre_timestamp:number, results: Article[]}>>('articles', { params: { channel_id: channelId, timestamp: Date.now() } })
    console.log(res)
    // res.data.data.results
    dispatch({
      type: 'article/addNewArticle',
      payload: {
        channelId: channelId,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}
